package com.hzz.web.boss.logs;

import com.hzz.api.log.ILogOperationService;
import com.hzz.dto.log.LogOperationDto;
import com.hzz.dto.user.UserDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.JwtUtil;
import com.hzz.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("员工操作日志")
@RestController
@Slf4j
public class LogOperationController extends BaseController {
    private static final String acPrefix = "/boss/logs/logOperation/";
    @Autowired
    ILogOperationService logOperationService;

    @RequiresPermissions("alog:menu")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "page/{pageNum}")
    // @ApiOperation(value = "信息分页(未删除)")
    public Response page(@ModelAttribute LogOperationDto dto, @PathVariable("pageNum") Integer pageNum) {
        log.info("LogOperationController page.........");
        Response result = new Response();
        try {
            dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
            dto.setPageNum(pageNum);
            UserDto staffDto = JwtUtil.getSubject(UserDto.class);
            dto.setStaffId(staffDto.getId());
            dto.setIsSuper(staffDto.getIsSuper());
            result.data = PageUtil.copy(logOperationService.findDataIsPage(dto));
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("alog:menu")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "options")
    // @ApiOperation(value = "获取options")
    public Response options() {
        log.info("LogOperationController options.........");
        Response result = new Response();
        try {
            result.data = logOperationService.options();
            result.setMessage("success");
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("alog:info")
    @RequestMapping(method = {RequestMethod.POST}, value = acPrefix + "info/{id}")
    // @ApiOperation(value = "信息详情")
    public Response info(@PathVariable("id") Long id) {
        log.info("LogOperationController info.........");
        Response result = new Response();
        try {
            LogOperationDto dto = new LogOperationDto() {{
                setId(id);
                UserDto staffDto = JwtUtil.getSubject(UserDto.class);
                setStaffId(staffDto.getId());
                setIsSuper(staffDto.getIsSuper());
            }};
            result.data = logOperationService.findDataById(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("alog:phydel")
    @RequestMapping(method = RequestMethod.POST, value = acPrefix + "phydel/{id}")
    // @ApiOperation(value = "物理删除")
    public Response phydel(@PathVariable("id") Long id) {
        log.info("LogOperationController phydel.........");
        Response result = new Response(0, "success");
        try {
            LogOperationDto dto = new LogOperationDto() {{
                setId(id);
                UserDto staffDto = JwtUtil.getSubject(UserDto.class);
                setStaffId(staffDto.getId());
                setIsSuper(staffDto.getIsSuper());
            }};
            result.message = logOperationService.deleteData(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }
}